<%
# Copyright (C) 2011 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
%>

<%
    show_google_docs = @assignment.allow_google_docs_submission? &&
                       @real_current_user.blank? &&
                       @domain_root_account &&
                       feature_and_service_enabled?(:google_drive)
-%>
<%
  js_bundle :submit_assignment
  css_bundle :tinymce
  js_env({
    :SUBMIT_ASSIGNMENT => {
      :ALLOWED_EXTENSIONS => (@assignment.allowed_extensions || []),
      :ID => @assignment.id,
      :GROUP_ID_FOR_USER => @assignment.group_category.try(:group_for, @current_user).try(:id)
    }
  })
%>
<div style="display: none;" id="submit_assignment" data-context_code="<%= @assignment.context_code %>" data-asset_string="<%= @assignment.asset_string %>">
  <div class="content" id="submit_assignment_tabs">
    <ul>
      <% if @assignment.submission_types && @assignment.submission_types.match(/online_upload/) %>
        <li><a href="#submit_online_upload_form" class="submit_online_upload_option"><%= t 'links.submit.file_upload', "File Upload" %></a></li>
      <% end %>
      <% if @assignment.submission_types && @assignment.submission_types.match(/online_text_entry/) %>
        <li><a href="#submit_online_text_entry_form" class="submit_online_text_entry_option"><%= t 'links.submit.text_entry', "Text Entry" %></a></li>
      <% end %>
      <% if @assignment.submission_types && @assignment.submission_types.match(/online_url/) %>
        <li><a href="#submit_online_url_form" class="submit_online_url_option"><%= t 'links.submit.website', "Website URL" %></a></li>
      <% end %>
      <% if show_google_docs %>
        <li><a href="#submit_google_doc_form" class="submit_online_url_option"><%= t 'links.submit.google_doc', 'Google Doc' %></a></li>
      <% end %>
      <% if @assignment.submission_types && @assignment.submission_types.match(/media_recording/) %>
        <li><a href="#submit_media_recording_form" class="submit_media_recording_option"><%= t 'links.submit.media', "Media" %></a></li>
      <% end %>
      <% (@external_tools[0,3] || []).each do |external_tool| %>
        <li><a href="#submit_from_external_tool_form_<%= external_tool.id %>"
          class="external-tool"
          data-id="<%= external_tool.id %>"
          data-name="<%= external_tool.name %>"><%= external_tool.label_for(:homework_submission, I18n.locale) %></a></li>
      <% end %>
      <% if @external_tools.length > 3 %>
        <li><a href="#submit_from_external_tool_form" class="submit_from_external_tool_option"><%= t 'links.submit.external_tool', "More" %></a></li>
      <% end %>
    </ul>
    <% if @assignment.submission_types && @assignment.submission_types.match(/online_upload/) %>
    <%= form_tag(context_url(@context, :controller => :submissions, :assignment_id => @assignment.id, :action => :create), {:id => "submit_online_upload_form", :class => "submit_assignment_form", :multipart => true }) do %>
    <%= hidden_field :submission, :submission_type, :value => "online_upload" %>
    <%= hidden_field :submission, :attachment_ids, :value => "", :id => "submission_attachment_ids" %>
    <table class="formtable" style="width: 100%;">
      <tr>
        <td colspan="2">
          <%= t :upload_or_choose_file, "Upload a file, or choose a file you've already uploaded." %>
            {{> views/assignments/group_submission_reminder}}
        </td>
      </tr><tr>
        <td><%= blabel :attachment, :uploaded_data, :en => "File", id: 'attachmentLabel' %></td>
        <td>
          <a href="#" class="add_another_file_link">
            <i class="icon-add" aria-hidden="true"></i>
            <%= t 'links.add_another_file', "Add Another File" %>
          </a>
          <div id="submission_attachment_blank" class="submission_attachment" style="display: none;">
            <input aria-labelledby="attachmentLabel" type="file" name="attachments[-1][uploaded_data]" class="input-file"/>
            <a href="#" class="remove_attachment_link no-hover" style="display: none;"><i class="icon-end" aria-hidden="true"></i></a>
            <% if @assignment.allowed_extensions.present? -%>
              <div class='bad_ext_msg error_message' style="display: none">
                <%= t 'errors.unacceptable_file_type', "This file type is not allowed. Accepted file types are: %{allowed_extensions_list}", :allowed_extensions_list => @assignment.allowed_extensions.to_sentence %>
              </div>
            <% end -%>
          </div>
        </td>
      </tr>
      <% unless @current_user.attachments.active.empty? %>
      <tr>
        <td></td>
        <td style="font-size: 0.8em;">
        <a href="#" class="toggle_uploaded_files_link"><%= t 'links.find_file', "Click here to find a file you've already uploaded" %></a><br/>
        <div id="uploaded_files" style="display: none;">
          <% folders = @current_user.folders.active %>
          <% content = (render :partial => "files/nested_content", :object => (folders.select{|f| !f.parent_folder_id}), :locals => {:folders => folders}) %>
          <%= t(:no_files, "No Files") if @current_user.attachments.active.empty? || !content || content.strip.empty? %>
          <%= content %>
        </div>
        </td>
      </tr>
      <% end %>
      <tr>
        <td colspan="2">
          <%= text_area :submission, :comment, :class => 'submission_comment_textarea', :placeholder => t('comments_placeholder', 'Comments...'), :title => t('additional_comments', 'Additional comments') %>
        </td>
      </tr>
      {{> views/group_comment}}
      <% if @assignment.turnitin_enabled? %>
        {{> views/turnitin}}
      <% end %>
      <% if @assignment.vericite_enabled? %>
        {{> views/vericite}}
      <% end %>
      <% if @assignment.tool_settings_tool.present? ||
         @assignment.vericite_enabled? ||
         @assignment.turnitin_enabled? %>
        <tr>
          <td colspan="2">
            <label class='checkbox'>
              <input type="checkbox" class='turnitin_pledge' name="turnitin_pledge" value="1"/>
              <%= @similarity_pledge %>
            </label>
          </td>
        </tr>
      <% end %>
      <tr>
        <td colspan="2" class='button-container'>
          <button type="button" class='cancel_button btn'><%= t '#buttons.cancel', "Cancel" %></button>
          <button type="submit" class="btn btn-primary" id="submit_file_button"><%= t 'buttons.submit_assignment', "Submit Assignment" %></button>
        </td>
      </tr><tr style="display: none;">
        <td colspan="2" style="text-align: center;">
          <%= t 'messages.submitting', "Submitting..." %> <img src=""/>
        </td>
      </tr>
    </table>
    <% end %>
    <% end %>

    <% if @assignment.submission_types && @assignment.submission_types.match(/online_text_entry/) %>
    <%= form_tag(context_url(@context, :controller => :submissions, :assignment_id => @assignment.id, :action => :create), {:id => "submit_online_text_entry_form", :class => "submit_assignment_form"}) do %>
    <%= hidden_field :submission, :submission_type, :value => "online_text_entry" %>
    <table class="formtable" style="width: 100%;">
      <tr>
        <td>
        <%= t 'instructions.online_text_entry', "Copy and paste or type your submission right here." %>
            {{> views/assignments/group_submission_reminder}}
        </td>
      </tr><tr>
        <td>
          <div style="text-align: right; font-size: 0.8em;">
            <a class="switch_text_entry_submission_views" href="#"><%= t '#editor.switch_editor_html', "HTML Editor" %></a>
            <a class="switch_text_entry_submission_views" href="#" style="display:none"><%= t '#editor.switch_editor_rich_text', "Rich Content Editor" %></a>
          </div>
          <%= text_area :submission, :body, :style => "width: 100%; height: 250px;" %>
        </td>
      </tr><tr>
        <td>
          <%= text_area :submission, :comment, :class => 'submission_comment_textarea', :placeholder => t('comments_placeholder', 'Comments...'), :title => t('additional_comments', 'Additional comments') %>
        </td>
      </tr>
      {{> views/group_comment}}
      <% if @assignment.turnitin_enabled? %>
        {{> views/turnitin}}
      <% end %>
      <% if @assignment.vericite_enabled? %>
        {{> views/vericite}}
      <% end %>
      <tr>
        <td class='button-container'>
          <button type="button" class='cancel_button btn'><%= t '#buttons.cancel', "Cancel" %></button>
          <button type="submit" class="btn btn-primary"><%= t 'buttons.submit_assignment', "Submit Assignment" %></button>
        </td>
      </tr>
    </table>
    <% end %>
    <% end %>

    <% if @assignment.submission_types && @assignment.submission_types.match(/online_url/) %>
    <%= form_tag(context_url(@context, :controller => :submissions, :assignment_id => @assignment.id, :action => :create), {:id => "submit_online_url_form", :class => "submit_assignment_form"}) do %>
    <%= hidden_field :submission, :submission_type, :value => "online_url" %>
    <table class="formtable" style="width: 100%;">
      <tr>
        <td colspan="2">
          <%= t 'instructions.online_url', "Copy and paste the link to the web site you'd like to submit for this assignment." %>
            {{> views/assignments/group_submission_reminder}}
          </td>
      </tr><tr>
        <td colspan="2">
          <%= blabel :submission, :url, :en => "Website URL" %>
          &nbsp;&nbsp;
          <%= text_field :submission, :url, :style => "min-width: 250px;" %>
        </td>
      </tr><tr>
        <td colspan="2" style="text-align: center;">
          <div style="text-align: left;">
            <%= text_area :submission, :comment, :class => 'submission_comment_textarea', :placeholder => t('comments_placeholder', 'Comments...'), :title => t('additional_comments', 'Additional comments') %>
          </div>
        </td>
      </tr>
      {{> views/group_comment}}
      <% if @assignment.turnitin_enabled? %>
        {{> views/turnitin}}
      <% end %>
      <% if @assignment.vericite_enabled? %>
        {{> views/vericite}}
      <% end %>
      <tr>
        <td colspan="2" class='button-container'>
          <button type="button" class='cancel_button btn'><%= t '#buttons.cancel', "Cancel" %></button>
          <button type="submit" class="btn btn-primary"><%= t 'buttons.submit_assignment', "Submit Assignment" %></button>
        </td>
      </tr>
    </table>
    <% end %>
    <% end %>

    <style>
    #google_docs_tree li.folder {
      cursor: pointer;
    }
    #google_docs_tree li.file {
      cursor: pointer;
      -moz-border-radius: 3px;
      width: 80%;
      padding-right: 20px;
    }
    #google_docs_tree li.file:hover {
      background-color: #eee;
    }
    ul.instTree li span.active {
      background: none;
    }
    #google_docs_tree li.file.leaf.active {
      background-color: #ddd;
    }
    #google_docs_tree li.file .filename {
      float: left;
      max-width: 98%;
    }
    #google_docs_tree li.file .popout {
      float: right;
    }
    </style>
    <% if show_google_docs %>
      <% if @user_has_google_drive %>
        <% if @domain_root_account.feature_enabled?(:google_docs_domain_restriction) &&
              @domain_root_account.settings[:google_docs_domain] &&
             !@current_user.gmail.match(%r{@#{@domain_root_account.settings[:google_docs_domain]}$}) %>
          <div id="submit_google_doc_form">
            <p class="alert alert-error">
              <i class="icon-warning" aria-hidden="true"></i>
              {{#t}}Invalid domain{{/t}}
            </p>

           <p>
            <%= t(:gmail_restriction_description, <<-END, domain: @domain_root_account.settings[:google_docs_domain])
              Your account has restricted Google Doc submissions to Google accounts
              on the %{domain} domain. To submit this assignment with a Google Doc,
              you will need to reconfigure the Google Docs integration on your
              user settings page.
            END
            %>
           </p>
            </div>
         <% else %>
          <%= form_tag(context_url(@context, :controller => :submissions, :assignment_id => @assignment.id, :action => :create), {:id => "submit_google_doc_form", :class => "submit_assignment_form"}) do %>
            <%= hidden_field :submission, :submission_type, :value => "google_doc" %>
            <%= hidden_field :google_doc, :document_id, :value => "", :class => "google_doc_id" %>
            <table class="formtable" style="width: 100%;">
              <tr>
                <td style="padding-bottom: 10px;" colspan="2">
                  <%= t 'instructions.google_docs', "Select the file from the list below." %>
                  {{> views/assignments/group_submission_reminder}}
                  </td>
              </tr><tr>
                <td colspan="2">
                  <div id="google_docs_container" style="height: 200px; overflow: auto;">
                    <div style="text-align: center; margin: 10px;">
                      <%= image_tag "ajax-loader-bar.gif", :alt => '' %>
                    </div>
                  </div>
                </td>
              </tr><tr>
                <td colspan="2" style="text-align: center;">
                  <div style="text-align: left;">
                    <%= text_area :submission, :comment, :class => 'submission_comment_textarea', :placeholder => t('comments_placeholder', 'Comments...'), :title => t('additional_comments', 'Additional comments') %>
                  </div>
                </td>
              </tr>
              {{> views/group_comment}}
              <% if @assignment.turnitin_enabled? %>
                {{> views/turnitin}}
              <% end %>
              <% if @assignment.vericite_enabled? %>
                {{> views/vericite}}
              <% end %>
              <tr>
                <td colspan="2" class='button-container'>
                  <button type="button" class='cancel_button btn'><%= t '#buttons.cancel', "Cancel" %></button>
                  <button type="submit" class="btn btn-primary"><%= t 'buttons.submit_assignment', "Submit Assignment" %></button>
                </td>
              </tr>
            </table>
            <div id="uploading_google_doc_message" style="display: none;">
              <%= t 'messages.uploading', "Retrieving a copy of your Google Doc to submit for this assignment.  This may take a little while, depending on the size of the file..." %>
              <div style="text-align: center; margin: 10px;">
                <%= image_tag "ajax-loader-bar.gif", :alt => '' %>
              </div>
            </div>
          <% end %>
        <% end %>
      <% else %>
        <div id="submit_google_doc_form">
          <%= t 'messages.google_drives_auth_required', "Before you can submit assignments directly from Google Drive you need to authorize Canvas to access your Google Drive account:" %>
          <div style="font-size: 1.1em; text-align: center; margin: 10px;">
            <a class="btn" href="<%= oauth_url(:service => :google_drive, :return_to => (request.url + "#submit_google_doc_form")) %>"><%= t 'links.authorize_google_drive', "Authorize Google Drive Access" %></a>
          </div>
        </div>
      <% end %>
    <% end %>

    <% if @assignment.submission_types && @assignment.submission_types.match(/media_recording/) %>
      <% if !feature_enabled?(:kaltura) %>
        <div id="submit_media_recording_form">
          <h3><%= t 'titles.media_recordings_disabled', "Media recordings are not enabled for this Canvas site" %></h3>
          <p><%= t 'messages.media_recordings_disabled', "Media recordings are not enabled for this Canvas site, and you will not be able to submit these types of assignments.  Please contact your instructor about changing the settings for this assignment, or your system administrator about enabling media recordings." %></p>
        </div>
      <% else %>
        <%= form_tag(context_url(@context, :controller => :submissions, :assignment_id => @assignment.id, :action => :create), {:id => "submit_media_recording_form", :class => "submit_assignment_form", :multipart => true }) do %>
        <%= hidden_field :submission, :submission_type, :value => "media_recording" %>
        <%= hidden_field :submission, :media_comment_id, :value => "", :class => "media_comment_id" %>
        <%= hidden_field :submission, :media_comment_type, :value => "video" %>
        <table class="formtable" style="width: 100%;">
          <tr>
            <td colspan="2" style="padding-bottom: 10px;">
              <%= t 'instructions.media_recording', "Use the link below to record your submission.  Click \"Save\" when you're finished and ready to submit." %>
                {{> views/assignments/group_submission_reminder}}
            </td>
          </tr><tr>
            <td>
              <div id="media_media_recording_submission_holder" style="font-size: 1.2em; font-weight: bold;">
                <div id="media_media_recording_ready" style="display: none; font-size: 1.5em;">
                  <%= t 'messages.media_recording_ready', "Ready to Submit!  Click 'Submit Assignment' button to finish."%>
                  <div id="media_media_recording_thumbnail">
                    <%= image_tag "media_comment.png", :alt => '' %>
                  </div>
                </div>
                <a href="#" class="record_media_comment_link btn" style="font-size: 1.1em;"><%= image_tag "record.gif", :alt => '' %> <%= t 'buttons.record_media', "Record / Upload Media" %></a>
                <div id="media_media_recording_submission" style="display: none; float: left; width: auto; border: 2px solid #444;">
                </div>
                <div class="clear"></div>
              </div>
            </td>
          </tr>
          <tr>
            <td colspan="2" style="text-align: center;">
              <div style="text-align: left;">
                <%= text_area :submission, :comment, :class => 'submission_comment_textarea', :placeholder => t('comments_placeholder', 'Comments...'), :title => t('additional_comments', 'Additional comments') %>
              </div>
            </td>
          </tr>
          {{> views/group_comment}}
          <tr>
            <td colspan="2" class='button-container'>
              <button type="button" class='cancel_button btn'><%= t '#buttons.cancel', "Cancel" %></button>
              <button type="submit" class="btn btn-primary" disabled="disabled" id="media_comment_submit_button"><%= t 'buttons.submit_assignment', "Submit Assignment" %></button>
            </td>
          </tr><tr style="display: none;">
            <td colspan="2" style="text-align: center;">
              <%= t 'messages.submitting', "Submitting..." %> <img src=""/>
            </td>
          </tr>
        </table>
       <% end %>
      <% end %>
    <% end %>

    <% (@external_tools || []).each do |external_tool| %>
      <div id="submit_from_external_tool_form_<%= external_tool.id %>" style="padding: 5px 5px 0 5px;"></div>
    <% end %>

    <% if @external_tools.length > 3 %>
      <%= form_tag(context_url(@context, :controller => :submissions, :assignment_id => @assignment.id, :action => :create), {:id => "submit_from_external_tool_form", :class => "submit_assignment_form" }) do %>
        <%= hidden_field :submission, :submission_type, :value => "online_url", :id => 'external_tool_submission_type' %>
        <%= hidden_field :submission, :filename, :id => 'external_tool_filename' %>
        <%= hidden_field :submission, :attachment_ids, :id => 'external_tool_file_id' %>
        <%= hidden_field :submission, :url, :id => 'external_tool_url' %>
        <%= hidden_field :submission, :content_type, :id => 'external_tool_content_type' %>
        <a href="<%= context_url(@context, :homework_submissions_context_external_tools_url) %>" id="external_tools_url"></a>
        <a href="/api/v1/courses/<%= @context.id %>/assignments/<%= @assignment.id %>/submissions/<%= @current_user.id %>/files" id="homework_file_url"></a>
        <table role="presentation" class="formtable" style="width: 100%;">
          <tr>
            <td colspan="2">
              <%= t 'instructions.from_external_tool', "Select a tool from the list below to load and submit a resource provided by that tool." %>
              {{> views/assignments/group_submission_reminder}}
            </td>
          </tr>
          <tr>
            <td colspan="2">
              <ul class='tools'>
                <li><%= t 'loading_tools', "Loading tools..." %></li>
              </ul>
            </td>
          </tr>
          <tr>
            <td colspan="2">
              {{#t}}Submission:{{/t}}
              <span id="external_tool_submission_details" class='not_selected'>
                {{#t}}No Submission Selected{{/t}}
              </span>
              <% if @assignment.allowed_extensions.present? -%>
                <div class='bad_ext_msg error_message' style="display: none">
                  <%= t 'errors.unacceptable_file_type', "This file type is not allowed. Accepted file types are: %{allowed_extensions_list}", :allowed_extensions_list => @assignment.allowed_extensions.to_sentence %>
                </div>
              <% end -%>
            </td>
          </tr><tr>
            <td colspan="2" style="text-align: center;">
              <div style="text-align: left;">
                <%= text_area :submission, :comment, :class => 'submission_comment_textarea', :style => "height: 16px;", :placeholder => t('comments_placeholder', 'Comments...'), :title => t('additional_comments', 'Additional comments') %>
              </div>
            </td>
          </tr>
          {{> views/group_comment}}
          <% if @assignment.turnitin_enabled? %>
            {{> views/turnitin}}
          <% end %>
          <% if @assignment.vericite_enabled? %>
            {{> views/vericite}}
          <% end %>
          <tr>
            <td colspan="2" class='button-container'>
              <button type="button" class="cancel_button btn"><%= t '#buttons.cancel', "Cancel" %></button>
              <button type="submit" class="btn btn-primary"><%= t 'buttons.submit_assignment', "Submit Assignment" %></button>
            </td>
          </tr>
        </table>
      <% end %>
    <% end %>
  </div>
</div>
